﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Platform;
using Oracle.DataAccess;
using System.Data;
using Oracle.DataAccess.Client;
using Platform.Data;

namespace Quartz.JobClass
{
    class SpHelper
    {
        #region Field
        private string ConnectionString;
        #endregion
        public SpHelper()
        {
            ConnectionString = Platform.Configuration.ConfigHelper.BusinessConnString;
            //ConnectionString = Platform.Security.SecurityHelper.DESEncrypt(ConnectionString, "shanghai3h");
        }
        /// <summary>
        /// 根据定义的SQL或存储过程获取数据源,改进版可以使用临时表的查询
        /// </summary>
        /// <param name="paramType"></param>
        /// <param name="sourceStr"></param>
        /// <returns></returns>
        public DataSet GetParamsDataSource(string paramType, string sourceStr, string extends)
        {
            DataSet ds = null;
            if (null == sourceStr)
                return null;

            // 存储过程数据格式
            string[] list = sourceStr.Split(';');

            if (null != list && list.Length >= 2)
            {
                OracleConnection connec = new OracleConnection(ConnectionString);
                using (connec)
                {
                    connec.Open();
                    OracleTransaction tran = connec.BeginTransaction(IsolationLevel.ReadCommitted);
                    try
                    {
                        if (paramType.Equals("<SQL>"))
                        {
                            ds = OracleHelper.ExecuteDataset(tran, CommandType.Text, list[1]);//.Tables[0];
                        }
                        else if (paramType.ToUpper().Equals("<PROCEDURE>"))
                        {
                            OracleParameter[] storedParams = Platform.Data.OracleHelperParameterCache.GetSpParameterSet(ConnectionString, list[1]);
                            for (int i = 0; i < storedParams.Length; i++)
                            {
                                if (storedParams[i].Direction == System.Data.ParameterDirection.Input)
                                    storedParams[i].Value = list[2 + i];
                            }

                            ds = OracleHelper.ExecuteDataset(tran, CommandType.StoredProcedure, list[1], storedParams);
                            //if (null == dst | dst.Tables.Count == 0)
                            //    return null;
                            //else
                            //    return dst.Tables[0];
                            tran.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != tran)
                        {
                            tran.Rollback();
                            throw new Exception(ex.Message.ToString());
                        }
                    }
                }

            }
            return ds;
        }

        public void Log(string S_GUID, string S_ZHUJIAN, string S_LEIXING, string S_JIEKOUMC, string TOKEN, string S_PRAMS, string CODE, string MSG, string S_CAOZUOR, string S_RIZHINR)
        {
            string strParms = "";
            strParms += S_GUID + ";";//S_GUID     
            strParms += S_ZHUJIAN+";";//S_ZHUJIAN  
            strParms += S_LEIXING+";";//S_LEIXING  
            strParms += S_JIEKOUMC+";";//S_JIEKOUMC 
            strParms += TOKEN + ";";//TOKEN      
            strParms += S_PRAMS + ";";//S_PRAMS    
            strParms += CODE + ";";//errCode    
            strParms += MSG + ";";//errMsg     
            strParms += S_CAOZUOR+";";//S_CAOZUOR
            strParms += "";//S_RIZHINR
            GetParamsDataSource("<PROCEDURE>", "UPDATE;GSC_PSETLOG;" + strParms, "");
        }

        /// <summary>
        /// 数据 
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="oracleConnection"></param>
        /// <returns></returns>
        public bool OracleBulkCopy(DataTable dataTable)
        {
            using (Oracle.DataAccess.Client.OracleConnection oracleConnection = new OracleConnection(Platform.Configuration.ConfigHelper.BusinessConnString))
            {
                oracleConnection.Open();
                Oracle.DataAccess.Client.OracleBulkCopy oracleBulkCopy = new Oracle.DataAccess.Client.OracleBulkCopy(oracleConnection);
                //{
                oracleBulkCopy.DestinationTableName = dataTable.TableName;
                try
                {
                    oracleBulkCopy.WriteToServer(dataTable);
                    return true;
                }
                catch(Exception ex)
                {
                    LogHelper.Error("批量导入{0}失败：{1}",dataTable.TableName, ex.Message);
                    return false;
                }
                finally
                {
                }
                //}
            }
        }
    }
}
